//
// Created by francklinson on 2021/8/29 AT 0:00.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <functional>
#include <climits>

using namespace std;

class Solution {
public:
    int surfaceArea(vector<vector<int>> &grid) {
        // 对每一个方块进行分析
        int dr[4]{0, 1, 0, -1};
        int dc[4]{1, 0, -1, 0};
        int N = (int)grid.size();
        int ans = 0;
        for (int r = 0; r < N; ++r) {
            for (int c = 0; c < N; ++c) {
                if (grid[r][c] > 0) {
                    ans += 2;  // 上 下面的贡献
                    for (int k = 0; k < 4; ++k) {
                        // 和四周进行对比，得到侧面的贡献
                        int nr = r + dr[k];
                        int nc = c + dc[k];
                        int nv = 0;
                        if (0 <= nr && nr < N && 0 <= nc && nc < N) {
                            nv = grid[nr][nc];
                        }
                        ans += max(grid[r][c] - nv, 0);
                    }
                }
            }
        }
        return ans;
    }
};

int main() {
    vector<vector<int>> grid{{1, 2},
                             {3, 4}};
    Solution solution;
    cout << solution.surfaceArea(grid) << endl;
    return 0;
}

